package com.ydh.company.controller;

import com.ydh.company.dto.ResponseData;
import com.ydh.company.dto.ResultData;
import com.ydh.company.pojo.*;
import com.ydh.company.utils.MD5Util;
import com.ydh.company.utils.UUIDUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("client")
@Api(value = "客户信息管理接口",tags = "客户信息管理接口API（9个）HKill")
public class ClientController {


    @PostMapping(value = "/queryClientByPage")
    @ResponseBody
    @ApiOperation(value = "1客户列表分页查询接口", notes = "展示初始列表信息", httpMethod = "POST", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败")}
    )
    public ResultData queryClientByPage(@RequestBody @ApiParam(name = "Condition", value = "传入页码和页容量", required = true) Condition condition) {

//        Map<String,Object> resultMap = staffService.selectStaffByPage(pageEntity);
        Map<String,Object> resultMap = new HashMap<>();

        //获取当前页码
        int currentPage = condition.getCurrentPage();
        //获取页容量
        int perSize = condition.getPerSize();
        System.out.println("当前页码：" + currentPage);
        System.out.println("页容量：" + perSize);

        List<Client> clientList = new ArrayList<>();

        Client client01 = new Client();
        client01.setClientId(UUIDUtil.getId());
        client01.setClientCompany("阿里巴巴集团");
        client01.setClientLevelId(1);
        client01.setUsername("alibaba123458");
        client01.setRepositoryId(UUIDUtil.getId());
        client01.setClientAddress("浙江杭州皮革城地区");
        client01.setDeptId(UUIDUtil.getId());
        client01.setClientName("马云老师");
        client01.setClientTel("138881111");
        client01.setCreateTime(new Timestamp(System.currentTimeMillis()));
        client01.setStateId(1);

        Client client02 = new Client();
        client02.setClientId(UUIDUtil.getId());
        client02.setClientCompany("京东自营商城");
        client02.setClientLevelId(1);
        client02.setUsername("JD12345448");
        client02.setRepositoryId(UUIDUtil.getId());
        client02.setClientAddress("江苏宿迁");
        client02.setDeptId(UUIDUtil.getId());
        client02.setClientName("刘强东");
        client02.setClientTel("138882222");
        client02.setCreateTime(new Timestamp(System.currentTimeMillis()));
        client02.setStateId(1);

        Client client03 = new Client();
        client03.setClientId(UUIDUtil.getId());
        client03.setClientCompany("苏宁易购");
        client03.setClientLevelId(1);
        client03.setUsername("suning123458");
        client03.setRepositoryId(UUIDUtil.getId());
        client03.setClientAddress("四川成都");
        client03.setDeptId(UUIDUtil.getId());
        client03.setClientName("周鸡儿东");
        client03.setClientTel("1383333");
        client03.setCreateTime(new Timestamp(System.currentTimeMillis()));
        client03.setStateId(1);

        clientList.add(client01);
        clientList.add(client02);
        clientList.add(client03);


        resultMap.put("totalCounts",35);
        resultMap.put("clientList", clientList);

        return ResponseData.success(resultMap);
    }




    @PostMapping(value = "queryClientLevel")
    @ResponseBody
    @ApiOperation(value = "2查询客户级别接口", notes = "展示所有客户级别名称", httpMethod = "POST", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败")}
    )
    public ResultData queryClientLevel(@RequestBody @ApiParam(name = "ClientLevel", value = "显示所有客户级别名称", required = true) ClientLevel clientLevel) {
        List<ClientLevel> clientLevelList = new ArrayList<>();

        ClientLevel clientLevel1 = new ClientLevel();
        clientLevel1.setClientLevelId(1);
        clientLevel1.setClientLevelName("普通代理");

        ClientLevel clientLevel2 = new ClientLevel();
        clientLevel2.setClientLevelId(2);
        clientLevel2.setClientLevelName("一级代理");

        ClientLevel clientLevel3 = new ClientLevel();
        clientLevel3.setClientLevelId(3);
        clientLevel3.setClientLevelName("二级代理");

        ClientLevel clientLevel4 = new ClientLevel();
        clientLevel4.setClientLevelId(4);
        clientLevel4.setClientLevelName("三级代理");

        clientLevelList.add(clientLevel1);
        clientLevelList.add(clientLevel2);
        clientLevelList.add(clientLevel3);
        clientLevelList.add(clientLevel4);

        return ResponseData.success(clientLevelList);
    }

    @PostMapping(value = "/queryClientByCondition")
    @ResponseBody
    @ApiOperation(value = "3客户列表条件查询接口", notes = "展示条件查询列表信息", httpMethod = "POST", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败")}
    )
    public ResultData queryClientByCondition(@RequestBody @ApiParam(name = "Condition", value = "输入条件，进行查询", required = true) Condition condition) {

        Map<String,Object> resultMap = new HashMap<>();

        //获取当前页码
        int currentPage = condition.getCurrentPage();
        //获取页容量
        int perSize = condition.getPerSize();
        //获取下拉框的值--客户级别
        String clientLevelId = condition.getSelectValue();
        //获取输入框的值
        String inputValue = condition.getInputValue();

        /*//获取客户公司
        String clientCompany = client.getClientCompany();
        //获取联系人
        String clientName = client.getClientName();
        //获取手机号
        String clientTel = client.getClientTel();*/

        System.out.println("当前页码：" + currentPage);
        System.out.println("页容量：" + perSize);
        System.out.println("下拉框：" + clientLevelId);
        System.out.println("输入框：" + inputValue);

        List<Client> clientList = new ArrayList<>();

        Client client01 = new Client();
        client01.setClientId(UUIDUtil.getId());
        client01.setClientCompany("阿里巴巴集团");
        client01.setClientLevelId(1);
        client01.setUsername("alibaba123458");
        client01.setRepositoryId(UUIDUtil.getId());
        client01.setClientAddress("浙江杭州皮革城地区");
        client01.setDeptId(UUIDUtil.getId());
        client01.setClientName("马云老师");
        client01.setClientTel("138881111");
        client01.setCreateTime(new Timestamp(System.currentTimeMillis()));
        client01.setStateId(1);

        Client client02 = new Client();
        client02.setClientId(UUIDUtil.getId());
        client02.setClientCompany("京东自营商城");
        client02.setClientLevelId(1);
        client02.setUsername("JD12345448");
        client02.setRepositoryId(UUIDUtil.getId());
        client02.setClientAddress("江苏宿迁");
        client02.setDeptId(UUIDUtil.getId());
        client02.setClientName("刘强东");
        client02.setClientTel("138882222");
        client02.setCreateTime(new Timestamp(System.currentTimeMillis()));
        client02.setStateId(1);

        Client client03 = new Client();
        client03.setClientId(UUIDUtil.getId());
        client03.setClientCompany("苏宁易购");
        client03.setClientLevelId(1);
        client03.setUsername("suning123458");
        client03.setRepositoryId(UUIDUtil.getId());
        client03.setClientAddress("四川成都");
        client03.setDeptId(UUIDUtil.getId());
        client03.setClientName("周鸡儿东");
        client03.setClientTel("1383333");
        client03.setCreateTime(new Timestamp(System.currentTimeMillis()));
        client03.setStateId(1);

        clientList.add(client01);
        clientList.add(client02);
        clientList.add(client03);


        resultMap.put("totalPage",22);
        resultMap.put("clientList", clientList);

        return ResponseData.success(resultMap);
    }




    @PostMapping(value = "queryRepository")
    @ResponseBody
    @ApiOperation(value = "4查询仓库名称接口", notes = "展示所有启用仓库名称", httpMethod = "POST", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败")}
    )
    public ResultData queryRepository(@RequestBody @ApiParam(name = "Repository", value = "显示所有启用仓库名称", required = true) Repository repository) {
        List<Repository> repositoryList = new ArrayList<>();

        Repository repository1 = new Repository();
        repository1.setRepositoryId(UUIDUtil.getId());
        repository1.setRepositoryName("成都仓");

        Repository repository2 = new Repository();
        repository2.setRepositoryId(UUIDUtil.getId());
        repository2.setRepositoryName("北京仓");

        Repository repository3 = new Repository();
        repository3.setRepositoryId(UUIDUtil.getId());
        repository3.setRepositoryName("上海仓");

        Repository repository4 = new Repository();
        repository4.setRepositoryId(UUIDUtil.getId());
        repository4.setRepositoryName("深圳仓");

        repositoryList.add(repository1);
        repositoryList.add(repository2);
        repositoryList.add(repository3);
        repositoryList.add(repository4);

        return ResponseData.success(repositoryList);
    }




    @PostMapping(value = "queryDept")
    @ResponseBody
    @ApiOperation(value = "5查询部门名称接口", notes = "展示所有部门名称", httpMethod = "POST", response = String.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败")}
    )
    public ResultData queryDept(@RequestBody @ApiParam(name = "Repository", value = "显示所有部门名称", required = true) Dept dept) {
        List<Dept> deptList = new ArrayList<>();

        Dept dept1 = new Dept();
        String uuid = UUIDUtil.getId();
        dept1.setDeptId(uuid);
        dept1.setDeptName("华东分公司");
        dept1.setDeptLevel(1L);
        dept1.setDeptParentId("");

        Dept dept2 = new Dept();
        dept2.setDeptId(UUIDUtil.getId());
        dept2.setDeptName("总部区域");
        dept2.setDeptLevel(1L);
        dept2.setDeptParentId("");

        Dept dept3 = new Dept();
        dept3.setDeptId(UUIDUtil.getId());
        dept3.setDeptName("华东分公司");
        dept3.setDeptLevel(2L);
        dept3.setDeptParentId(uuid);

        Dept dept4 = new Dept();
        dept4.setDeptId(UUIDUtil.getId());
        dept4.setDeptName("深圳分公司");
        dept4.setDeptLevel(2L);
        dept4.setDeptParentId(uuid);

        deptList.add(dept1);
        deptList.add(dept2);
        deptList.add(dept3);
        deptList.add(dept4);

        return ResponseData.success(deptList);
    }



    @PostMapping(value = "/addClient")
    @ResponseBody
    @ApiOperation(value = "6新增客户信息接口",notes = "把客户信息存放在数据库中",httpMethod = "POST",response = String.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData addClient(@RequestBody @ApiParam(name = "Client",value = "新增客户信息",required = true) Client client){

        String clientId = UUIDUtil.getId();
        String clientCompany = client.getClientCompany();
        int clientLevelId = client.getClientLevelId();
        String repositoryId = client.getRepositoryId();
        String deptId = client.getDeptId();
        String clientAddress = client.getClientAddress();
        String clientName = client.getClientName();
        String clientTel = client.getClientTel();
        String username = client.getUsername();
        String password = MD5Util.MD55(client.getPassword());
        Timestamp createTime = new Timestamp(System.currentTimeMillis());
        int stateId = client.getStateId();

        System.out.println("================客户信息=====================");
        System.out.println("客户id：" + clientId);
        System.out.println("客户公司名称：" + clientCompany);
        System.out.println("客户级别：" + clientLevelId);
        System.out.println("仓库id：" + repositoryId);
        System.out.println("部门id：" + deptId);
        System.out.println("客户地址：" + clientAddress);
        System.out.println("联系人：" + clientName);
        System.out.println("联系电话：" + clientTel);
        System.out.println("登陆账号：" + username);
        System.out.println("密码：" + password);
        System.out.println("创建时间：" + createTime);
        System.out.println("状态：" + stateId);

        String fianceMoneyId = UUIDUtil.getId();
        String fianceMoneyCompany = client.getFiance().getFianceMoneyCompany();
        String taxpayerNumber = client.getFiance().getTaxpayerNumber();
        String address = client.getFiance().getAddress();
        String tel = client.getFiance().getTel();
        String accountName = client.getFiance().getAccountName();
        String bank = client.getFiance().getBank();
        String bankNumber = client.getFiance().getBankNumber();

        System.out.println("==================客户财务信息===================");
        System.out.println("财务表id：" + fianceMoneyId);
        System.out.println("发票抬头：" + fianceMoneyCompany);
        System.out.println("纳税人识别号：" + taxpayerNumber);
        System.out.println("地址：" + address);
        System.out.println("电话：" + tel);
        System.out.println("开户名称：" + accountName);
        System.out.println("开户银行：" + bank);
        System.out.println("银行账号：" + bankNumber);
        System.out.println("=====================================");


        int count = 1;
        if (count == 1){
            return ResponseData.success(count);
        }else {
            return ResponseData.error(400,"新增失败");
        }

    }


    @PostMapping(value = "/updateClient")
    @ResponseBody
    @ApiOperation(value = "7修改客户信息接口",notes = "对客户信息进行编辑",httpMethod = "POST",response = String.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData updateClient(@RequestBody @ApiParam(name = "Client",value = "通过客户id（编码）,修改客户信息",required = true) Client client){

        String clientId = client.getClientId();
        String clientCompany = client.getClientCompany();
        int clientLevelId = client.getClientLevelId();
        String repositoryId = client.getRepositoryId();
        String deptId = client.getDeptId();
        String clientAddress = client.getClientAddress();
        String clientName = client.getClientName();
        String clientTel = client.getClientTel();
        String username = client.getUsername();
        String password = client.getPassword();
        int stateId = client.getStateId();

        System.out.println("================客户信息=====================");
        System.out.println("客户id：" + clientId);
        System.out.println("客户公司名称：" + clientCompany);
        System.out.println("客户级别：" + clientLevelId);
        System.out.println("仓库id：" + repositoryId);
        System.out.println("部门id：" + deptId);
        System.out.println("客户地址：" + clientAddress);
        System.out.println("联系人：" + clientName);
        System.out.println("联系电话：" + clientTel);
        System.out.println("登陆账号：" + username);
        System.out.println("密码：" + password);
        System.out.println("状态：" + stateId);


        String fianceMoneyCompany = client.getFiance().getFianceMoneyCompany();
        String taxpayerNumber = client.getFiance().getTaxpayerNumber();
        String address = client.getFiance().getAddress();
        String tel = client.getFiance().getTel();
        String accountName = client.getFiance().getAccountName();
        String bank = client.getFiance().getBank();
        String bankNumber = client.getFiance().getBankNumber();

        System.out.println("==================客户财务信息===================");
        System.out.println("发票抬头：" + fianceMoneyCompany);
        System.out.println("纳税人识别号：" + taxpayerNumber);
        System.out.println("地址：" + address);
        System.out.println("电话：" + tel);
        System.out.println("开户名称：" + accountName);
        System.out.println("开户银行：" + bank);
        System.out.println("银行账号：" + bankNumber);
        System.out.println("=====================================");


        int count = 1;
        if (count == 1){
            return ResponseData.success(count);
        }else {
            return ResponseData.error(400,"修改失败");
        }

    }





    @PostMapping(value = "/judgeClientOrderMain")
    @ResponseBody
    @ApiOperation(value = "8判断客户是否存在已发生订单接口",notes = "判断客户是否存在已发生订单",httpMethod = "POST",response = String.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData judgeClientOrderMain(@RequestBody @ApiParam(name = "OrderMain",value = "通过客户id（编码）,查询订单",required = true) OrderMain orderMain){

        String clientId = orderMain.getClientId();

        System.out.println("================客户信息=====================");
        System.out.println("客户id：" + clientId);
        System.out.println("=====================================");

        int count = 0;
        if (count == 0){
            return ResponseData.success("");
        }else {
            return ResponseData.error(400,"该客户已有订单发生，不允许删除！");
        }

    }




    @PostMapping(value = "/delClient")
    @ResponseBody
    @ApiOperation(value = "9删除客户信息接口",notes = "删除客户信息",httpMethod = "POST",response = String.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResultData delClient(@RequestBody @ApiParam(name = "Client",value = "通过客户id（编码）,删除客户信息",required = true) Client client){

        String clientId = client.getClientId();

        System.out.println("================客户信息=====================");
        System.out.println("客户id：" + clientId);
        System.out.println("=====================================");

        int count = 1;
        if (count == 1){
            return ResponseData.success(count);
        }else {
            return ResponseData.error(400,"删除失败！");
        }

    }



}
